package com.acxiom.bsh.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acxiom.bsh.constants.CommonConstant;
import com.acxiom.bsh.dao.LuckydrawDao;
import com.acxiom.bsh.pojo.LltPojo;
import com.acxiom.bsh.pojo.PrizeRecord;
import com.acxiom.bsh.service.ApiService;
import com.acxiom.bsh.util.CommonUtils;
import com.acxiom.bsh.util.LltUtil;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping(value = "/llt")
public class LltApiController {

	@Autowired
	ApiService apiService;
	
	@Autowired
	LuckydrawDao luckyDrawDao;
	
	/**
	 * 流量AppKey
	 */
	@Value("${llt.appKey}")
	private String appKey;
	
	@Value("${llt.account}")
	private String account;
	
	/**
	 * 开始充值
	 * @param tel
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/rechargeFlow")
	public String rechargeFlow(@RequestParam(name = "tel") String tel,HttpSession session) {
		String mixNick = getNickName(session);
		JSONObject json = new JSONObject();
		if (CommonUtils.isEmpty(mixNick) || CommonUtils.isEmpty(tel)) {
			json.put("code", "emptyError");
			json.put("msg", "参数错误");
			return json.toString();
		}
		//查询奖品信息
		PrizeRecord pr = new PrizeRecord();
		pr.setMixNick(mixNick);
		pr.setPrizeId(13);
		PrizeRecord prizeRecord = luckyDrawDao.getPrizeRecordByLLt(pr);
		if(null==prizeRecord || prizeRecord.getMixNick()==null){
			json.put("code", "dataError");
			json.put("msg", "数据错误");
			return json.toString();
		}
		LltPojo lltPojo = new LltPojo();
		lltPojo.setAccount(account);
		lltPojo.setMobile(tel);
		lltPojo.setAppKey(appKey);
		String lltResult = LltUtil.rechargeFlow(lltPojo);
		JSONObject lltJson = JSONObject.parseObject(lltResult);		
		if(lltJson.get("Code").toString().equals("0")){
			luckyDrawDao.updatePrizeStatus(13, 2,mixNick);
		}else{
			luckyDrawDao.updatePrizeStatus(13, 1,mixNick);
		}
		lltJson.put("code", "success");
		lltJson.put("msg", "充值成功");
		return lltJson.toString(); 
	}
	
	
	/**
	 * 获取用户nick信息
	* @Title: getNickName
	* @param session 存储淘宝用户信息会话
	* @return
	 */
	private String getNickName(HttpSession session) {
		Object obj = session.getAttribute(CommonConstant.TAOBAO_NICK_ATTRIBUTE);
		String nickName = null;
		if (obj != null) {
			nickName = obj.toString();
		} 
		return nickName;
	}
	
}
